<?php
namespace app\admin\controller\v1;
use app\admin\Adminbase;
use app\common\model\Shop as ShopModel;
use base\date\Dates;

class ShopScheduling extends Adminbase 
{
	/**
	* 获取店铺排班设置数据
	* /admin/v1/shop_scheduling/getData
	* 需要登录 
	* @is_need_login true
	*/
	public function getData($shop_id='',$date=''){
		$months = explode("-",$date);
		if(!$months[1]){
			return $this->ReError('参数错误！');	
		}
		$year = $months[0];
		$month = $months[1];
		try {
	    	$model = model('ShopStaff')->alias('a');
			$model = $model->leftJoin("shop_scheduling b","
				b.shop_id = a.shop_id and a.id = b.staff_id and b.year=$year and b.month=$month
			");
			$model = $model->field("a.shop_id,a.id as staff_id,a.name as staff_name");
			$model = $model->field("ifnull(b.year,'$year')  as year");
			$model = $model->field("ifnull(b.month,'$month')  as month");
			$model = $model->field("ifnull(b.days,'')  as days");
			$model = $model->where("a.shop_id",$shop_id);
			$model = $model->where("a.is_del",0);
			
			$data=[
				"dates"=> Dates::getMonthDays( strtotime( date($date.'-01'))),
				"staff"=>$model->all()
			];
			
			return $this->ReSucess(  $data);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 获取店铺排班设置数据
	* /admin/v1/shop_scheduling/saveData
	* 需要登录 
	* @is_need_login true
	*/
	public function saveData($data){
		try {
			$data = json_decode($data,true);
			model('shop_scheduling')
				->where('shop_id',$data[0]['shop_id'])
				->where('year',$data[0]['year'])
				->where('month',$data[0]['month'])
				->delete();
	    	model('shop_scheduling')->allowField(true)->saveAll($data);
			return $this->ReSucess(  );	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
}



